from drf_haystack.serializers import HaystackSerializer
from rest_framework import serializers

from goods.models import GoodsChannel, GoodsCategory, SKU
from goods.search_indexes import SKUIndex


class CategorySerializer(serializers.ModelSerializer):
    """商品类别序列化器"""

    class Meta:
        model = GoodsCategory
        fields = ('id', 'name')


class ChannelSerializer(serializers.ModelSerializer):
    """频道序列化器"""
    category = CategorySerializer(read_only=True)

    class Meta:
        model = GoodsChannel
        fields = ('url','category')


class SKUSerializer(serializers.ModelSerializer):
    """
    SKU序列化器
    {
    "count": 14,
    "next": "http://api.meiduo.site:8000/categories/115/skus/?page=2",
    "previous": null,
    "results": [
        {
            "id": 3,
            "name": "Apple iPhone 8 Plus (A1864) 64GB 金色 移动联通电信4G手机",
            "price": "6499.00",
            "default_image_url": "http://image.meiduo.site:8888/group1/M00/00/02/CtM3BVrRZCqAUxp9AAFti6upbx41220032",
            "comments": 0
        },
        {
            "id": 4,
            "name": "Apple iPhone 8 Plus (A1864) 256GB 金色 移动联通电信4G手机",
            "price": "7988.00",
            "default_image_url": "http://image.meiduo.site:8888/group1/M00/00/02/CtM3BVrRZa6ANO_sAAFti6upbx40753757",
            "comments": 0
        }
    ]
    }
    """

    class Meta:
        model = SKU
        fields = ('id', 'name', 'price', 'default_image_url', 'comments', 'sales')


class SKUIndexSerializer(HaystackSerializer):
    """
    SKU索引结果数据序列化器
    """
    class Meta:
        index_classes = [SKUIndex]
        fields = ('text', 'id', 'name', 'price', 'default_image_url', 'comments')
